package com.orie.librarybase.base;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.orie.librarybase.R;
import com.orie.librarybase.utils.StatusBarUtil;
import com.vondear.rxui.view.dialog.RxDialogLoading;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseActivity extends AppCompatActivity {

    protected String TAG = this.getClass().getSimpleName() + "打印日志";

    protected abstract int provideContentViewId();

    private boolean isVerticalScreen = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        StatusBarUtil.transparencyBar(this);
//        StatusBarUtil.setColor(this, R.color.main_color);
//        StatusBarUtil.StatusBarLightMode(this);
        beforeInit();
        setContentView(provideContentViewId());
        initView(savedInstanceState);
        initEvent();
        initScreen();

    }

    private void initScreen() {
//        if (isVerticalScreen) {
//            //始终保持竖屏
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }
    }

    /**
     * 初始化Toolbar
     */
    protected void initToolbar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    private RxDialogLoading rxDialogLoading;

    /**
     * 显示加载弹出框
     */
    protected void showLoadingDialog() {
        if (rxDialogLoading == null) {
            rxDialogLoading = new RxDialogLoading(this);
            rxDialogLoading.setCanceledOnTouchOutside(false);
        }
        rxDialogLoading.show();
    }

    /**
     * 带值的加载弹出框
     *
     * @param msg
     */
    protected void showLoadingDialog(String msg) {
        if (rxDialogLoading == null) {
            rxDialogLoading = new RxDialogLoading(this);
        }
        rxDialogLoading.setLoadingText(msg);
        rxDialogLoading.show();
    }

    /**
     * 隐藏加载弹出框
     */
    public void dismissLoadingDialog() {
        if (rxDialogLoading != null && rxDialogLoading.isShowing()) {
            rxDialogLoading.dismiss();
        }
    }

    /**
     * 初始化布局以及View控件
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 初始化数据事件
     */
    protected abstract void initEvent();

    /**
     * 界面初始化前期准备
     */
    protected abstract void beforeInit();

    /**
     * 替换页面
     *
     * @param containerViewId
     * @param fragment
     * @param tag
     */
    public void replaceFragment(int containerViewId, Fragment fragment, String tag) {
        if (null == getSupportFragmentManager().findFragmentByTag(tag)) {
            getSupportFragmentManager().beginTransaction()
                    .replace(containerViewId, fragment, tag)
                    .commit();
        }
    }

    /**
     * 申请权限封装
     */
    private PermissionListener mListener;
    private static final int PERMISSION_REQUESTCODE = 100;

    //是否授权
    public void requestRunPermisssion(String[] permissions, PermissionListener listener) {
        mListener = listener;
        List<String> permissionLists = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionLists.add(permission);
            }
        }
        if (!permissionLists.isEmpty()) {
            ActivityCompat.requestPermissions(this, permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUESTCODE);
        } else {
            //表示全都授权了
            mListener.onGranted();
        }
    }

    /**
     * 已授权、未授权的接口回调
     */
    public interface PermissionListener {

        void onGranted();//已授权

        void onDenied(List<String> deniedPermission);//未授权

    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        switch (requestCode) {
//            case PERMISSION_REQUESTCODE:
//                if (grantResults.length > 0) {
//                    //存放没授权的权限
//                    List<String> deniedPermissions = new ArrayList<>();
//                    for (int i = 0; i < grantResults.length; i++) {
//                        int grantResult = grantResults[i];
//                        String permission = permissions[i];
//                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
//                            deniedPermissions.add(permission);
//                        }
//                    }
//                    if (deniedPermissions.isEmpty()) {
//                        //说明都授权了
//                        mListener.onGranted();
//                    } else {
//                        mListener.onDenied(deniedPermissions);
//                    }
//                }
//                break;
//            default:
//                break;
//        }
//    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    //是否保持竖屏
    public boolean isVerticalScreen() {
        return isVerticalScreen;
    }

    //是否保持竖屏
    public void setVerticalScreen(boolean verticalScreen) {
        isVerticalScreen = verticalScreen;
    }
}
